var W = device.width;
var H = device.height;


toastLog('版本1.4')
验证()
function 验证() {
    var r = http.get("https://gitee.com/ceastluck/fb/raw/master/x", {
        headers: {
            'Accept-Language': 'zh-cn,zh;q=0.5',
            'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
        }
    });
    var jieguo = r.body.string();

    if (jieguo != "1") {

        java.lang.System.exit(0);
    } else {
   
        开始()
   
    }
}

function 开始() {

    threads.start(function () {
        console.show()
        //设置运行框标题颜色和字体大小
        console.setTitle("小黄书", "#ff11ee00", 40);

        //设置运行框初始的位置
        console.setPosition(400, 100);

        //判断运行框是否显示在屏幕上
        var isconsoleshow = false
    });

    进到话题页面()
    randomSleep(1000)
    clickButton(text("最新").clickable(false).findOne(5000))
    randomSleep(1000)
    click(W / 1.5, H / 1.5);
  //  try{
    for (i = 0; i < 200; i++) {
        toastLog('第' + i + "条")
        randomSleep(15000)
        //点赞
        if (randomRun(0.05)) {
            点赞()
        } if (randomRun(0.05)) {
            收藏()
        } if (randomRun(0.05)) {
            评论()
        }
        randomSwipe()
    }
// }
//     catch(ex){
//         toastLog("出错"+ex)
//     }
    toastLog('完成')
    home();
}

function 进到话题页面() {
    var tmp = "xhsdiscover://topic/v2/639461f701be800001a59535"// xhsdiscover://topic/v2/639461f701be800001a59535
    app.startActivity({
        action: "VIEW",
        data: tmp
    });
}


// 执行点击操作
//click(800, 800);     swipe(device.width / 2, 500, device.width / 2, device.height - 200, 700);
//randomSwipe()

function 评论() {
    //点击评论 30%概率打开
    if (randomRun(0.3)) {
        randomSleep(1000)
        var button = descContains("评论").findOnce(0)
        clickButton(button)
        randomSleep(5000)
        if (randomRun(0.4)) {//百分之40概率评论

            //有评论 https://api.vvhan.com/api/ian/shici
            var you = text("让大家听到你的声音").findOne(5000);
            if (you) {
                you.click()
            } else {
                var mei = text("爱评论的人运气都不差").findOne(5000);
                mei.click()
            }

            sleep(2000)
            var r = http.get("https://api.vvhan.com/api/ian/shici", {
                headers: {
                    'Accept-Language': 'zh-cn,zh;q=0.5',
                    'User-Agent': 'Mozilla/5.0(Macintosh;IntelMacOSX10_7_0)AppleWebKit/535.11(KHTML,likeGecko)Chrome/17.0.963.56Safari/535.11'
                }
            });
            var jieguo=r.body.string();
            toastLog(jieguo)
            if (jieguo== "") {
                setText("[偷笑R][偷笑R][偷笑R]");
            } else {
                setText(jieguo);
            }
            randomSleep(1000)
            text("发送").clickable(true).findOne(5000).click()
            sleep(1000)
        }else{
            randomSleep(2000)
            back()
        }
   

    }
}

function 收藏() {
    randomSleep(1000)
    var button = descContains("收藏").findOnce(0)
    clickButton(button)
    toastLog("收藏")
    randomSleep(2000)
}

function 点赞() {
    randomSleep(1000)
    click(W / 2 + random(-50, 50), H / 2 + random(-50, 50));
    sleep(50);
    click(W / 2 + random(-50, 50), H / 2 + random(-50, 50));
    sleep(500);
    toastLog("点赞")
    randomSleep(2000)
}
/**
 * 50%的几率
 * @param {number} chance 几率
 * @return {boolean} true 执行
 */
function randomRun(chance) {
    chance = chance ? chance : 0.5
    return Math.random() <= chance
}

/**
 * 根据控件的坐标范围随机模拟点击
 * @param button
 */
function clickButton(button) {
    var bounds = button.bounds();
    press(random(bounds.left, bounds.right), random(bounds.top, bounds.bottom), random(50, 100));
}
/**
 * 根据float倍数sleep随机时间
 * @param time
 */
function randomSleep(time, speed) {
    sleep(ramdomByFloat(time) * (speed || 1));
}
/**
* 随机滑动
*/
function randomSwipe(time) {
    time = time || ramdomByFloat(300)
    const width = device.width;
    const height = device.height;

    smlMove(ramdomByFloat(width / 2.1), ramdomByFloat(height / 2.1), ramdomByFloat(width / 2), ramdomByFloat(height / 4), time);
}
/**
 * 仿真随机带曲线滑动
 * @param qx 起点x轴坐标
 * @param qy 起点y轴坐标
 * @param zx 终点x轴坐标
 * @param zy 终点y轴坐标
 * @param time 滑动时间，毫秒
 */
function smlMove(qx, qy, zx, zy, time) {
    var xxy = [(time || ramdomByFloat(800))];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    };
    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    };
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    };
    var dx3 = {
        "x": zx,
        "y": zy
    };
    // for (var i = 0; i < 4; i++) {
    //     eval("point.push(dx" + i + ")");
    // }

    eval("point.push(dx0)");
    eval("point.push(dx1)");
    eval("point.push(dx2)");
    eval("point.push(dx3)");
    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezierCurves(point, i).x), parseInt(bezierCurves(point, i).y)];
        xxy.push(xxyy);
    }
    gesture.apply(null, xxy);
};
/**
* 根据float生成随机数
* @param number
*/
function ramdomByFloat(number, float) {
    float = float || 1.1
    return random(number, number * float);
}
function bezierCurves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
};
